apiVersion: monitoring.coreos.com/v1
kind: Alertmanager
metadata:
  labels:
    app.kubernetes.io/component: alert-router
    app.kubernetes.io/instance: main
    app.kubernetes.io/name: alertmanager
    app.kubernetes.io/part-of: kube-prometheus
    app.kubernetes.io/version: {{ alertmanager_tag | replace("v", "") }}
  name: main
  namespace: kubesphere-monitoring-system
spec:
{% if monitoring.affinity is defined and monitoring.affinity is not none %}
  affinity:
    {{  monitoring.affinity | to_nice_yaml(indent=2) | indent(4) }}
{% elif affinity is defined and affinity is not none %}
  affinity:
    {{ affinity | to_nice_yaml(indent=2) | indent(4) }}
{% else %}
  affinity:
    podAntiAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - podAffinityTerm:
          labelSelector:
            matchExpressions:
            - key: alertmanager
              operator: In
              values:
              - main
          namespaces:
          - kubesphere-monitoring-system
          topologyKey: kubernetes.io/hostname
        weight: 100
{% endif %}
{% if monitoring.nodeSelector is defined and monitoring.nodeSelector is not none %}
  nodeSelector:
    {{ monitoring.nodeSelector | to_nice_yaml(indent=2) | indent(4) }}
{% elif nodeSelector is defined and nodeSelector is not none %}
  nodeSelector:
    {{ nodeSelector | to_nice_yaml(indent=2) | indent(4) }}
{% else %}
  nodeSelector:
    kubernetes.io/os: linux
{% endif %}
{% if monitoring.tolerations is defined and monitoring.tolerations is not none %}
  tolerations:
    {{ monitoring.tolerations | to_nice_yaml(indent=2) | indent(4) }}
{% elif tolerations is defined and tolerations is not none %}
  tolerations:
    {{ tolerations | to_nice_yaml(indent=2) | indent(4) }}
{% else %}
  tolerations: []
{% endif %}
  image: {{ alertmanager_repo }}:{{ alertmanager_tag }}
  podMetadata:
    labels:
      app.kubernetes.io/component: alert-router
      app.kubernetes.io/instance: main
      app.kubernetes.io/name: alertmanager
      app.kubernetes.io/part-of: kube-prometheus
      app.kubernetes.io/version: {{ alertmanager_tag | replace("v", "") }}
  resources:
    limits:
      cpu: {{ monitoring.alertmanager.resources.limits.cpu | default(monitoring.alertmanager.limits.cpu) | default("200m") }}
      memory: {{ monitoring.alertmanager.resources.limits.memory | default(monitoring.alertmanager.limits.memory) | default("200Mi") }}
    requests:
      cpu: {{ monitoring.alertmanager.resources.requests.cpu | default(monitoring.alertmanager.requests.cpu) | default("20m") }}
      memory: {{ monitoring.alertmanager.resources.requests.memory | default(monitoring.alertmanager.requests.memory) | default("30Mi") }}

{% if nodeNum is defined and nodeNum < 3 %}
  replicas: {{ monitoring.alertmanager.replicas | default(monitoring.alertmanagerReplicas) | default(1) }}
{% else %}
  replicas: {{ monitoring.alertmanager.replicas | default(monitoring.alertmanagerReplicas) | default(3) }}
{% endif %}
  securityContext:
    fsGroup: 2000
    runAsNonRoot: true
    runAsUser: 1000
  serviceAccountName: alertmanager-main
  version: {{ alertmanager_tag }}
